package org.tea.inventory.db.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.tea.inventory.db.dao.QuestionDetailMapper;
import org.tea.inventory.db.dao.QuestionSortMapper;
import org.tea.inventory.db.domain.QuestionDetail;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PK的问题
 */
@Service
public class QuestionDetailService {

    @Resource
    private QuestionDetailMapper questionDetailMapper;
    @Resource
    private QuestionSortMapper questionSortMapper;

    public List<QuestionDetail> selectRandom(Integer sortId,Integer num){
        return questionDetailMapper.selectRandom(sortId,num);
    }
    public List<QuestionDetail> selectRandom(Integer num){
        return questionDetailMapper.selectRandom2(num);
    }

    public Map<String, Object> selectByExample(QuestionDetail record,Integer page,Integer size){

        PageHelper.startPage(page, size);
        List<QuestionDetail> list = null;
        list = questionDetailMapper.selectByExample(record);
        PageInfo pageinfo = new PageInfo(list);
        Map<String,Object> resMap = new HashMap<String,Object>();
        resMap.put("pageNum",pageinfo.getPageNum());
        resMap.put("pageSize", pageinfo.getPageSize());
        resMap.put("startRow", pageinfo.getStartRow());
        resMap.put("endRow", pageinfo.getEndRow());
        resMap.put("total", pageinfo.getTotal());
        resMap.put("pages", pageinfo.getPages());
        resMap.put("items", list);
        return resMap;
    }

    public int deleteByPrimaryKey(Integer id){
        QuestionDetail questionDetail = questionDetailMapper.selectByPrimaryKey(id);
        //更新问题数量
        int temp = questionDetailMapper.deleteByPrimaryKey(id);
        questionSortMapper.updateQuestionNumByPrimaryKey(questionDetail.getSortId());
        return temp;
    }

    public int insert(QuestionDetail record){
        int temp = questionDetailMapper.insert(record);
        questionSortMapper.updateQuestionNumByPrimaryKey(record.getSortId());
        return temp;
    }

    public int insertSelective(QuestionDetail record){
        int temp = questionDetailMapper.insertSelective(record);
        questionSortMapper.updateQuestionNumByPrimaryKey(record.getSortId());
        return temp;
    }

    public QuestionDetail selectByPrimaryKey(Integer id){
        return questionDetailMapper.selectByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(QuestionDetail record){
        //原数据
        QuestionDetail questionDetail = questionDetailMapper.selectByPrimaryKey(record.getId());
        int temp = questionDetailMapper.updateByPrimaryKeySelective(record);
        //现数据
        Integer sortId = record.getSortId();
        //两个值不同，分别修改两个的统计量
        if(sortId != null && sortId != 0 && sortId.intValue() != questionDetail.getSortId().intValue()){
            questionSortMapper.updateQuestionNumByPrimaryKey(sortId);
            questionSortMapper.updateQuestionNumByPrimaryKey(questionDetail.getSortId());
        }
        return temp;
    }

    public int updateByPrimaryKey(QuestionDetail record){
        //原数据
        QuestionDetail questionDetail = questionDetailMapper.selectByPrimaryKey(record.getId());
        int temp = questionDetailMapper.updateByPrimaryKey(record);
        //现数据
        Integer sortId = record.getSortId();
        //两个值不同，分别修改两个的统计量
        if(sortId != null && sortId != 0 && sortId.intValue() != questionDetail.getSortId().intValue()){
            questionSortMapper.updateQuestionNumByPrimaryKey(sortId);
            questionSortMapper.updateQuestionNumByPrimaryKey(questionDetail.getSortId());
        }

        return temp;
    }
}
